package com.ruoyi.training.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.training.vo.payment.FundPoolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.FundPool;
import com.ruoyi.training.service.IFundPoolService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 资金池Controller
 * 
 * @author hcc
 * @date 2025-09-28
 */
@RestController
@RequestMapping("/pool")
public class FundPoolController extends BaseController
{
    @Autowired
    private IFundPoolService fundPoolService;

    /**
     * 查询资金池列表
     */
    @RequiresPermissions("training:pool:list")
    @GetMapping("/list")
    public TableDataInfo list(FundPool fundPool)
    {
        startPage();
        List<FundPool> list = fundPoolService.selectFundPoolList(fundPool);
        return getDataTable(list);
    }
    /**
     * 查询资金池列表
     */
    @RequiresPermissions("training:pool:list")
    @GetMapping("/getList")
    public TableDataInfo getList(FundPoolVo fundPoolVo)
    {
        startPage();
        List<FundPool> list = fundPoolService.selectList(fundPoolVo);
        return getDataTable(list);
    }

    /**
     * 导出资金池列表
     */
    @RequiresPermissions("training:pool:export")
    @Log(title = "资金池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FundPool fundPool)
    {
        List<FundPool> list = fundPoolService.selectFundPoolList(fundPool);
        ExcelUtil<FundPool> util = new ExcelUtil<FundPool>(FundPool.class);
        util.exportExcel(response, list, "资金池数据");
    }

    /**
     * 获取资金池详细信息
     */
    @RequiresPermissions("training:pool:query")
    @GetMapping(value = "/{fundPoolId}")
    public AjaxResult getInfo(@PathVariable("fundPoolId") String fundPoolId)
    {
        return success(fundPoolService.selectFundPoolByFundPoolId(fundPoolId));
    }

    /**
     * 新增资金池
     */
    @RequiresPermissions("training:pool:add")
    @Log(title = "资金池", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FundPool fundPool)
    {
        if (StringUtils.isNotEmpty(fundPool.getPoolTransactionNum())){
            // 通过交易号查询资金池
            FundPool fundPoolDb = fundPoolService.selectFundPoolByPoolTransactionNum(fundPool.getPoolTransactionNum());
            if (StringUtils.isNotNull(fundPoolDb)){
                return error("交易号" + fundPool.getPoolTransactionNum() + "已存在");
            }
        }
        return toAjax(fundPoolService.insertFundPool(fundPool));
    }

    /**
     * 修改资金池
     */
    @RequiresPermissions("training:pool:edit")
    @Log(title = "资金池", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FundPool fundPool)
    {
        return toAjax(fundPoolService.updateFundPool(fundPool));
    }

    // TODO
    /**
     * 出账
     */
    @RequiresPermissions("training:pool:edit")
    @Log(title = "资金池", businessType = BusinessType.UPDATE)
    @PutMapping("/disbursement")
    public AjaxResult disbursement(@RequestBody FundPool fundPool)
    {
        return toAjax(fundPoolService.disbursement(fundPool));
    }

    // TODO
    /**
     * 出账并合并发票
     */
    @RequiresPermissions("training:pool:edit")
    @Log(title = "资金池", businessType = BusinessType.UPDATE)
    @PutMapping("/consolidateInvoice")
    public AjaxResult consolidateInvoice(@RequestBody FundPool fundPool)
    {
        return toAjax(fundPoolService.consolidateInvoice(fundPool));
    }

    /**
     * 删除资金池
     */
    @RequiresPermissions("training:pool:remove")
    @Log(title = "资金池", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fundPoolIds}")
    public AjaxResult remove(@PathVariable String[] fundPoolIds)
    {
        return toAjax(fundPoolService.deleteFundPoolByFundPoolIds(fundPoolIds));
    }

    /**
     * 逻辑删除资金池
     */
    @RequiresPermissions("training:pool:edit")
    @Log(title = "资金池", businessType = BusinessType.UPDATE)
    @PutMapping("/removeFundPool/{fundPoolIds}")
    public AjaxResult removeFundPool(@PathVariable String[] fundPoolIds)
    {
        return toAjax(fundPoolService.removeFundPool(fundPoolIds));
    }

    /**
     * 获取资金池关联报名信息
     */
    @RequiresPermissions("training:pool:query")
    @GetMapping(value = "/getFundPoolCorrelationInfo/{fundPoolId}")
    public AjaxResult getFundPoolCorrelationInfo(@PathVariable("fundPoolId") String fundPoolId)
    {
        return success(fundPoolService.getFundPoolCorrelationInfo(fundPoolId));
    }

}
